package com.handpay.nocardpay.baselibrary;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.handpay.nocardpay.baselibrary.account.view.fragment.NoCardLoginFragment;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.activity.BaseActivity;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.main.mine.MainFragment;
import com.handpay.nocardpay.baselibrary.main.mine.presenter.MinePresenterImpl;
import com.handpay.nocardpay.baselibrary.notification.service.NoticeService;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;

import java.util.HashMap;

/**
 * Created by sxshi on 2017/8/9.
 */

public class DelegateActivity extends BaseActivity {

    public long TOUCH_TIME = 0;
    private Intent noticeIntent;
    private MinePresenterImpl minePresenter=null;
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (findFragment(MainFragment.class) == null) {
            loadRootFragment(R.id.fl_container, MainFragment.newInstance());
        }
    }

    @Override
    public void initData() {
        minePresenter=new MinePresenterImpl(this);
        startNoticeService();
        submitMerchStatistical();
    }
    //提交商户信息
    private void submitMerchStatistical(){
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put(BaseProtocolKey.PLATFORM, "Android");
        params.put(BaseProtocolKey.TUDID, ClientEngine.getInstance().getDeviceUuid());
        params.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        params.put(BaseProtocolKey.CLIENTCHANNEL, ClientEngine.getInstance().CLIENTCHANNEL);
        params.put(BaseProtocolKey.CLIENTVERSION, ClientEngine.getInstance().VERSION);
        minePresenter.submitMarchData(params);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_delegate;
    }

    //登入后拉起通知服务
    private void startNoticeService() {
        noticeIntent = new Intent(this, NoticeService.class);
        startService(noticeIntent);
    }

    @Override
    protected void onDestroy() {
        stopService(noticeIntent);
        if (minePresenter!=null){
            minePresenter.onDestory();
        }
        super.onDestroy();
    }

    @Override
    public void onBackPressedSupport() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            pop();
        } else {
            if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                finish();
//                NoCardPayApplication.getApp().exitAppaction();
            } else {
                TOUCH_TIME = System.currentTimeMillis();
                DialogUtils.showToast(this, getString(R.string.press_again_exit));
            }
        }

    }
}
